package com.example.bysj.Dao;
//201902104061徐中堂
import com.example.bysj.domain.authority.Actor;
import com.example.bysj.domain.authority.ActorAssRole;
import com.example.bysj.Service.ActorService;
import com.example.bysj.Service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
//声明数据库访问类
@Repository("actorAssRoleDao")
public class ActorAssRoleDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private ActorService actorService;
    @Autowired
    private RoleService roleService;

    //增加
    public boolean add(ActorAssRole actorAssRole) {
        //sql语句
        String sql = "INSERT INTO actorAssRole " +
                "( actor_id, role_id) VALUES (?, ?)";
        //执行sql语句并返回执行记录
        //update可执行增加，修改，删除等语句
        int affectedRowNum = jdbcTemplate.update(sql, actorAssRole.getActor().getId(),
                actorAssRole.getRole().getId());
        if (affectedRowNum != 1) {
            return false;
        } else {
            return true;
        }
    }

    //通过定义内部类来返回查询结果
    public class ActorAssRoleRowMapper implements RowMapper<ActorAssRole> {
        @Override
        public ActorAssRole mapRow(ResultSet resultSet, int i) throws SQLException {
            //用查询返回的结果为对象赋值
            ActorAssRole actorAssRole = new ActorAssRole();
            actorAssRole.setId(resultSet.getInt("id"));
            actorAssRole.setActor(actorService.find(resultSet.getInt("actor_id")));
            actorAssRole.setRole(roleService.find(resultSet.getInt("role_id")));
            return actorAssRole;
        }
    }
    //根据id查询
    public ActorAssRole find(int id) {
        String sql = "SELECT * FROM ActorAssRole WHERE id = ?";
        //query方法执行查询
        List<ActorAssRole> actorAssRoles = jdbcTemplate.query(sql, new Object[]{id}, new ActorAssRoleRowMapper());
        if (!actorAssRoles.isEmpty()) {
            return actorAssRoles.get(0);
        } else
            return null;
    }
    //根据actorId查询
    public List<ActorAssRole> findByActorId(Actor actor) {
        String sql = "SELECT * FROM ActorAssRole WHERE actor_id = ?";
        List<ActorAssRole> actorAssRoles = jdbcTemplate.query(sql, new Object[]{actor.getId()}, new ActorAssRoleRowMapper());
        return actorAssRoles;
    }
    //查询全部
    public List<ActorAssRole> findAll() {
        String sql = "SELECT * FROM ActorAssRole ";
        List<ActorAssRole> actorAssRoles = jdbcTemplate.query(sql, new ActorAssRoleRowMapper());
        return actorAssRoles;
    }
    //修改
    public boolean update(ActorAssRole actorAssRole) {
        //sql语句
        String sql = "UPDATE actorAssRole SET actor_id = ?, role_id = ?" +
                " WHERE id = ?";
        //执行语句
        int affectedRowNum = jdbcTemplate.update(sql, actorAssRole.getActor().getId(),
                actorAssRole.getRole().getId(), actorAssRole.getId());
        if (affectedRowNum != 1) {
            return false;
        } else {
            return true;
        }
    }
    //删除
    public boolean delete(int id) {
        String sql = "DELETE FROM ActorAssRole WHERE id = ? ";
        int affectedRowNum = jdbcTemplate.update(sql, id);
        if (affectedRowNum != 1) {
            return false;
        } else {
            return true;
        }
    }
}
